package com.pzhu.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pzhu.entity.*;
import com.pzhu.service.PhotographService;
import com.pzhu.until.JwtTokenUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@RestController
@RequestMapping("api/photograph")
public class PhotographController {

    @Autowired
    private PhotographService photographService;

    /**
     * 分页条件查询摄影
     *
     * @param current
     * @param size
     * @param pName
     * @param pAddress
     * @return
     */
    @GetMapping
    public Result select(
            @RequestParam("current") Integer current,
            @RequestParam("size") Integer size,
            @RequestParam("pName") String pName,
            @RequestParam("pAddress") String pAddress) {


        LambdaQueryWrapper<Photograph> photographLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<Photograph> orderPage = new Page<>(current, size);

        photographLambdaQueryWrapper.like(pName != null, Photograph::getPName, pName);
        photographLambdaQueryWrapper.like(pAddress != null, Photograph::getPAddress, pAddress);

        Page<Photograph> page = photographService.page(orderPage, photographLambdaQueryWrapper);


        return new Result(Code.success, "", page);

    }

    /**
     * 用户查询摄影
     *
     * @param current
     * @param size
     * @return
     */

    @GetMapping("u")
    public Result select(
            @RequestParam("current") Integer current,
            @RequestParam("size") Integer size) {


        LambdaQueryWrapper<Photograph> photographLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<Photograph> orderPage = new Page<>(current, size);


        Page<Photograph> page = photographService.page(orderPage, photographLambdaQueryWrapper);


        return new Result(Code.success, "", page);

    }

    /**
     * 添加摄影
     *
     * @param request
     * @param photograph
     * @param multipartFile
     * @return
     * @throws IOException
     */
    @PostMapping
    public Result add(HttpServletRequest request, @RequestParam("photograph") String photograph, @RequestParam MultipartFile multipartFile) throws IOException {


        Photograph photograph1 = JSON.parseObject(photograph, Photograph.class);
        byte[] bytes = multipartFile.getInputStream().readAllBytes();

        Integer loginBusiness = JwtTokenUntil.getClaim(request, "loginBusiness").asInt();

        photograph1.setPBusinessId(loginBusiness);
        photograph1.setPImg(bytes);
        boolean save = photographService.save(photograph1);
        return new Result(Code.success, "", save);
    }

    /**
     * 删除摄影
     * @param photograph
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestBody Photograph photograph) {

        boolean b = photographService.removeById(photograph);
        return new Result(Code.success, "", b);

    }

    /**
     * 修改摄影包括图片
     * @param photograph
     * @param multipartFile
     * @return
     * @throws IOException
     */
    @PutMapping
    private Result update(@RequestParam("photograph") String photograph, @RequestParam MultipartFile multipartFile) throws IOException {

        Photograph photograph1 = JSON.parseObject(photograph, Photograph.class);
        byte[] bytes = multipartFile.getInputStream().readAllBytes();
        photograph1.setPImg(bytes);

        boolean b = photographService.updateById(photograph1);
        return new Result(Code.success, "", b);


    }

    /**
     * 修改摄影不包括图片
     * @param photograph
     * @return
     * @throws IOException
     */
    @PutMapping("edit")
    private Result update(@RequestParam("photograph") String photograph) throws IOException {

        Photograph photograph1 = JSON.parseObject(photograph, Photograph.class);
        boolean b = photographService.updateById(photograph1);
        return new Result(Code.success, "", b);
    }
}
